package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dl7.recycler.helper.RecyclerViewHelper;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.DownloadUtil;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyOutSaveManager2ZiZhiAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.EventEntityBean;
import patrol.mobile.com.mobilepatrol.bean.OutSaveManager2Bean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyEvent2Activity extends BaseActivity {


    @BindView(R.id.tv_eventName)
    TextView tvEventName;
    @BindView(R.id.tv_place)
    TextView tvPlace;
    @BindView(R.id.tv_startTime)
    TextView tvStartTime;
    @BindView(R.id.tv_orgName)
    TextView tvOrgName;
    @BindView(R.id.tv_reporter)
    TextView tvReporter;
    @BindView(R.id.tv_glaces)
    TextView tvGlaces;
    @BindView(R.id.spDangerGlaces)
    MySpinner spDangerGlaces;
    @BindView(R.id.et_brief)
    EditText etBrief;
    @BindView(R.id.tv_blockingUp)
    TextView tvBlockingUp;
    @BindView(R.id.tv_strand)
    TextView tvStrand;
    @BindView(R.id.tv_damnification)
    TextView tvDamnification;
    @BindView(R.id.tv_input)
    TextView tvInput;
    @BindView(R.id.spYes)
    MySpinner spYes;
    @BindView(R.id.spEmergency)
    MySpinner spEmergency;
    @BindView(R.id.tv_dispositionDate)
    TextView tvDispositionDate;
    @BindView(R.id.et_disposition)
    EditText etDisposition;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<String> xData = new ArrayList<>();
    private List<String> xYes = new ArrayList<>();
    private List<String> xEmergency = new ArrayList<>();
    private List<OutSaveManager2Bean> outSaveManager2BeanList = new ArrayList<>();
    private EmergencyOutSaveManager2ZiZhiAdapter emergencyOutSaveManager2ZiZhiAdapter;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_emergency_event2;
    }

    @Override
    public void setListener() {
        emergencyOutSaveManager2ZiZhiAdapter = new EmergencyOutSaveManager2ZiZhiAdapter(this);
        RecyclerViewHelper.initRecyclerViewH(this, recyclerView, emergencyOutSaveManager2ZiZhiAdapter);
        emergencyOutSaveManager2ZiZhiAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                final OutSaveManager2Bean ensureMade2Bean = outSaveManager2BeanList.get(position);
                final String url = baseUrl.substring(0, baseUrl.length() - 1) + ensureMade2Bean.getFilePath().replaceAll("\\\\", "/");
                final String path = "ensureMade";
                final String fileName = ensureMade2Bean.getFileName();
                DownloadUtil.get().download(url, Environment.getExternalStorageDirectory().getAbsolutePath(), fileName, new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        try {
                            File file2 = new File(file.getAbsolutePath());
                            Intent intent2 = new Intent("android.intent.action.VIEW");
                            intent2.addCategory("android.intent.category.DEFAULT");
                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            Uri uri = Uri.fromFile(file2);
                            //application/msword
                            //application/vnd.ms-excel
                            if (ensureMade2Bean.getFileType().contains("doc")) {
                                intent2.setDataAndType(uri, "application/msword");
                            } else if (ensureMade2Bean.getFileType().contains("xlsx")) {
                                intent2.setDataAndType(uri, "application/vnd.ms-excel");
                            } else {
                                intent2.setDataAndType(uri, "text/plain");
                            }
                            startActivity(intent2);
                        } catch (Exception e) {
                            //没有安装第三方的软件会提示
                            ToastUtils.showToast("没有找到打开该文件的应用程序");
                        }
                    }

                    @Override
                    public void onDownloading(int progress) {

                    }

                    @Override
                    public void onDownloadFailed(Exception e) {

                    }
                });
            }
        });
        xData.add("较小");
        xData.add("较大");
        xData.add("重大");
        xData.add("一般");
        xData.add("特重大");

        xYes.add("是");
        xYes.add("否");

        xEmergency.add("1");
        xEmergency.add("2");
        xEmergency.add("3");
        xEmergency.add("4");

        spDangerGlaces.setmDatas(xData, new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {

            }
        });

        spYes.setmDatas(xYes, new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {

            }
        });

        spEmergency.setmDatas(xEmergency, new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {

            }
        });

        requestData(getIntent().getStringExtra("id"));
    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EmergencyEvent2Activity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(ConsTitles.EMERGENCY_EVENT_2);
    }

    private void requestData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_EVENT_ENTITY);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response, String attachList) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                EventEntityBean eventEntityBean = GsonUtil.GsonToBean(response, EventEntityBean.class);
                tvEventName.setText(eventEntityBean.getEventName());
                tvPlace.setText(eventEntityBean.getPlace());
                tvStartTime.setText(eventEntityBean.getStartTime());
                tvOrgName.setText(eventEntityBean.getOrgName());
                tvReporter.setText(eventEntityBean.getReporter());
                tvGlaces.setText(eventEntityBean.getGlaces());
                etBrief.setText(eventEntityBean.getBrief());
                tvBlockingUp.setText(eventEntityBean.getBlockingUp());
                tvStrand.setText(eventEntityBean.getStrand());
                tvDamnification.setText(eventEntityBean.getDamnification());
                tvInput.setText(eventEntityBean.getInput() + "");
                tvDispositionDate.setText(eventEntityBean.getDispositionDate());
                etDisposition.setText(eventEntityBean.getDisposition());
                String dangerClass = eventEntityBean.getDangerGlaces();
                if (!TextUtils.isEmpty(dangerClass)) {
                    if ("较小".equals(dangerClass)) {
                        ((RadioButton) findViewById(R.id.rb_jiaoxiao)).setChecked(true);
                    } else if ("较大".equals(dangerClass)) {
                        ((RadioButton) findViewById(R.id.rb_jiaoda)).setChecked(true);
                    } else if ("重大".equals(dangerClass)) {
                        ((RadioButton) findViewById(R.id.rb_zhongda)).setChecked(true);
                    } else if ("一般".equals(dangerClass)) {
                        ((RadioButton) findViewById(R.id.rb_yiban)).setChecked(true);
                    } else if ("特重大".equals(dangerClass)) {
                        ((RadioButton) findViewById(R.id.rb_tezhongda)).setChecked(true);
                    }
                }
                String yes = eventEntityBean.getYes();
                if (!TextUtils.isEmpty(yes)) {
                    if ("是".equals(yes)) {
                        ((RadioButton) findViewById(R.id.rb_shi)).setChecked(true);
                    } else if ("否".equals(yes)) {
                        ((RadioButton) findViewById(R.id.rb_fou)).setChecked(true);
                    }
                }

                String emergency = eventEntityBean.getEmergency();
                if (!TextUtils.isEmpty(emergency)) {
                    if ("一".equals(emergency)) {
                        ((RadioButton) findViewById(R.id.rb_1)).setChecked(true);
                    } else if ("二".equals(emergency)) {
                        ((RadioButton) findViewById(R.id.rb_2)).setChecked(true);
                    } else if ("三".equals(emergency)) {
                        ((RadioButton) findViewById(R.id.rb_3)).setChecked(true);
                    } else if ("一四".equals(emergency)) {
                        ((RadioButton) findViewById(R.id.rb_4)).setChecked(true);
                    }
                }
                if (!TextUtils.isEmpty(attachList)){
                    outSaveManager2BeanList = GsonUtil.jsonToList(attachList, OutSaveManager2Bean.class);
                    emergencyOutSaveManager2ZiZhiAdapter.updateItems(outSaveManager2BeanList);
                }
//                if (attachList.length() > 10) {
//                    recyclerView.setVisibility(View.VISIBLE);
//                    List<ImageBean2> imageBeans = GsonUtil.jsonToList(attachList, ImageBean2.class);
//                    Log.e("________", new Gson().toJson(imageBeans));
//                    List<String> list = new ArrayList<>();
//                    for (ImageBean2 imageBean : imageBeans) {
//                        list.add((baseUrl.substring(0, baseUrl.length() - 1) + imageBean.getFilePath()).replace("\\", "/"));
//                    }
//                    recyclerView.setLayoutManager(new GridLayoutManager(EmergencyEvent2Activity.this, 4));
//                    ImageAdapter imageAdapter = new ImageAdapter(list);
//                    recyclerView.setAdapter(imageAdapter);
//                }
            }
        });
    }
}
